<template>
  <div class="app-container">
    <div class="zd3">
      <div class="zd3-title">
        <p>
          <span>部门</span>
          <input v-model="bumen" type="text" placeholder="请输入部门" />
        </p>
        <h3 @click="getList">查询</h3>
				<h4 @click="resetList">重置</h4>
				<h3 @click="addcompanys" style="background: #0093FF">新增公司</h3>
      </div>
      <div class="zd3-btm">
        <el-table
          :data="tableData"
          style="width: 100%;margin-bottom: 20px;"
          v-loading="loading"
        >
          <el-table-column type="index" align="center" label="序号" width="80" />
          <el-table-column type="expand">
					  <template slot-scope="props" v-if="props.row.departmentsList">
							<el-table :data="props.row.departmentsList" border style="width: 100%">
                <el-table-column type="index" align="center" label="序号" width="80" />
								<el-table-column align="center" prop="departments" label="部门名称"/>
								<el-table-column align="center" prop="updateTime" label="时间"/>
								<el-table-column align="center" width="250" label="操作">
                  <template slot-scope="scope">
                    <el-button type="text" size="small" @click="write(scope.row,scope.$index)">修改</el-button>
                    <el-popconfirm
                      placement="top"
                      hideIcon
                      title="是否要删除此行？"
                      @onConfirm="deleteRow(scope.row)"
                    >
                      <el-button type="text" size="small" slot="reference">删除</el-button>
                    </el-popconfirm>
                  </template>
                </el-table-column>
							</el-table>
					  </template>
					</el-table-column>
          <el-table-column align="center" prop="companyName" label="公司名称"></el-table-column>
          <el-table-column align="center" prop="personsNumbers" label="电话"></el-table-column>
          <el-table-column align="center" width="250" label="操作">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="addPeople(scope.row)">新增部门</el-button>
              <el-button type="text" size="small" @click="writeCompany(scope.row,scope.$index)">修改</el-button>
              <el-popconfirm
                placement="top"
                hideIcon
                title="是否要删除此行？"
                @onConfirm="deletecompanys(scope.row)"
              >
                <el-button type="text" size="small" slot="reference">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          hide-on-single-page
          layout="prev, pager, next"
          :total="total"
          @prev-click="goPrev"
          @next-click="goNext"
        />
      </div>
    </div>
    <el-dialog :visible.sync="dialogVisible" :close-on-click-modal="false" :modal-append-to-body="false" :modal="true" width="60%">
      <div class="change">
        <p>
          <span>部门名称</span>
          <input type="text" v-model="tempData.departments" placeholder="请输入部门名称" />
        </p>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消保存</el-button>
        <el-button type="primary" :disabled="isClick" @click="save">保存</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="companyVisible" :close-on-click-modal="false" :modal-append-to-body="false" :modal="true" width="60%">
      <div class="change">
        <p>
          <span>公司名称</span>
          <input type="text" v-model="companyData.companyName" placeholder="请输入公司名称" />
        </p>
        <p>
          <span>电话</span>
          <input type="text" v-model="companyData.personsNumbers" placeholder="请输入电话" />
        </p>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="companyVisible = false">取消保存</el-button>
        <el-button type="primary" :disabled="isClick" @click="saveCompany">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getBumen,getBumenNew,updateBumen,addBumen,deleteBumen,addCompany,updateCompany,deleteCompany } from '@/api/people'
export default {
  data() {
    return {
      name: "",
      bumen: "",
      total: 1,
      showHeader: false,
      tableData: [],
      dialogVisible: false,
      companyVisible: false,
      tempData: {},     //编辑部门承载数据
      companyData: {},  //编辑公司承载数据
      tempIndex: -1,
      companyIndex: -1,
      loading: true,
			isClick: false
    };
  },
  mounted(){
    this.getList()
  },
  methods: {
    changeValue3(value) {},
    getList() {
      const param = {
        departments: this.bumen || null
      }
      getBumenNew(param).then(res => {
        console.log('部门管理')
        console.log(res)
        this.tableData = res.data
        this.loading = false
      })
    },
    write(item, index) {
			this.tempData = {...item}
			this.dialogVisible = true
			this.tempIndex = index
    },
    /**
     * 保存新增部门
     */
    save() {
			this.isClick = true
			if(this.tempIndex == -1){
        // console.log('新增')
        addBumen(this.tempData).then(res => {
          console.log(res)
          this.$message({
            showClose: true,
            message: res.message,
            type: 'success'
          });
          this.getList()
					this.isClick = false
        })
			}else{
        updateBumen(this.tempData).then(res => {
          console.log(res)
          this.$message({
            showClose: true,
            message: res.message,
            type: 'success'
          });
					this.isClick = false
          this.getList()
        })
				// console.log('xiugai')
      }
      this.dialogVisible = false
			this.tempIndex = -1
    },
    /**
     * 保存新增部门分类
     */
    saveCompany(){
      this.isClick = true
			if(this.companyIndex == -1){
        // console.log('新增')
        addCompany(this.companyData).then(res => {
          console.log(res)
          this.$message({
            showClose: true,
            message: res.message,
            type: 'success'
          });
          this.getList()
					this.isClick = false
        })
			}else{
        updateCompany(this.companyData).then(res => {
          console.log(res)
          this.$message({
            showClose: true,
            message: res.message,
            type: 'success'
          });
					this.isClick = false
          this.getList()
        })
				// console.log('xiugai')
      }
      this.companyVisible = false
			this.companyIndex = -1
    },
    /**
     * 新增公司分类
     */
    addcompanys(){
      this.companyIndex = -1
      this.companyData = {}
      this.companyVisible = true
    },
    /**
     * 编辑公司分类
     */
    writeCompany(row,index){
      this.companyIndex = index
      this.companyData = {...row}
      this.companyVisible = true
    },
    /**
     * 删除公司分类
     */
    deletecompanys(item){
      deleteCompany(item.ids).then(res => {
        console.log(res)
        this.$message({
          showClose: true,
          message: res.message,
          type: 'success'
        });
        this.getList()
      })
    },
    // expend(row) {
    //   let $table = this.$refs.table;
    //   this.tableData.map(item => {
    //     if (row.id != item.id) {
    //       $table.toggleRowExpansion(item, false);
    //       item.expansion = false;
    //     } else {
    //       item.expansion = !item.expansion;
    //     }
    //   });
    //   $table.toggleRowExpansion(row);
    // },
    deleteRow(item) {
      console.log("删除");
      deleteBumen(item.id).then(res => {
        console.log(res)
        this.$message({
          showClose: true,
          message: res.message,
          type: 'success'
        });
        this.getList()
      })
    },
    goPrev(page) {
      console.log(page);
    },
    goNext(page) {
      console.log(page);
    },
    resetList() {
      this.bumen = ""
    },
    addPeople(row) {
      this.dialogVisible = true;
      this.tempIndex = -1;
			this.tempData = {
        parentId: row.ids
      }
    }
  }
};
</script>

<style lang="scss" scoped>
/deep/.el-table__expanded-cell[class*=cell] {
  padding: 10px 10px;
}
/deep/.el-button--primary {
  color: #fff;
  background-color: #18a05e !important;
  border-color: #18a05e !important;
}
/deep/.el-button--primary:hover,
.el-button--primary:focus {
  background: #18a05e !important;
  border-color: #18a05e !important;
  color: #fff !important;
}
/deep/.el-button:hover,
.el-button:focus {
  color: #18a05e;
  background-color: transparent;
}
/deep/.el-button--primary {
  color: #fff;
  background-color: #18a05e;
  border-color: #18a05e;
}
/deep/.el-button--text {
  color: #00c666 !important;
}

.app-container {
  padding: 10px;
}
.status {
  border-radius: 3px;
  font-size: 14px;
  width: 50px;
  color: #fff;
  background: #777777;
  margin: 0 auto;
}
.status-active {
  background: #18a05e;
}
.inps {
  width: 100%;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  background: none;
  border: none;
  color: #333;
}
.inps-active {
  width: 100%;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  // background:#ccc;
  border: none;
  color: #333;
}
.zd3-title {
	height: 80px;
	display: flex;
	align-items: center;
	padding: 0 20px;
	position: relative;
	background: #ffffff;
	border-radius: 8px;
	border: 1px solid rgba(0, 0, 0, 0.1);

	> p {
		font-size: 14px;
		color: #333;
		margin-right: 30px;

		> span {
			padding-right: 20px;
		}

		> input {
			width: 200px;
			height: 32px;
			// background: #ccc;
			border-radius: 3px;
			border: 1px solid #ccc;
			padding-left: 10px;
			color: #333;
		}
	}

	> h3 {
		width: 64px;
		height: 32px;
		background: #18a05e;
		border-radius: 3px;
		margin: 0 30px 0 0;
		color: #fff;
		font-size: 14px;
		line-height: 32px;
		font-weight: 100;
		text-align: center;
		cursor: pointer;
	}

	> h4 {
		width: 64px;
		height: 32px;
		border-radius: 3px;
		border: 1px solid rgba(0, 0, 0, 0.1);
		color: #333;
		font-size: 14px;
		line-height: 32px;
		font-weight: 100;
		text-align: center;
		cursor: pointer;
		margin-right: 30px;
	}
}
.zd3-btm .btn-prev:after {
  content: "上一页";
}
.zd3-btm .btn-next:before {
  content: "下一页";
}
.zd3-btm .btn-prev,
.zd3-btm .btn-next {
  width: 65px;
}
.zd3-btm {
  background: rgba(255, 255, 255, 1);
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  margin-top: 10px;
  overflow: hidden;
  &-title {
    display: flex;
    align-items: center;
    height: 70px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    background: #ffffff;
    > p {
      text-align: center;
      font-size: 16px;
      color: #333;
    }
  }
  .el-pagination {
		white-space: nowrap;
		padding: 15px 5px;
		color: #303133;
		font-weight: bold;
	}
  .addDate {
    margin: 20px 32px 10px 32px;
    height: 32px;
    border-radius: 3px;
    text-align: center;
    line-height: 32px;
    font-size: 14px;
    color: #333;
    cursor: pointer;
    border: 1px dashed rgba(0, 0, 0, 0.3);
  }
  .el-button--text {
    margin: 0 15px;
  }
}
.change {
  display: flex;
  flex-wrap: wrap;
  > p {
    width: 50%;
    font-size: 14px;
    display: flex;
    align-items: center;
    color: #333;
    /deep/.el-input__inner {
      width: inherit;
      background: none;
      height: 32px;
      // line-height: 32px;
      color: #333;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding: 0 25px;
    }
    /deep/.el-input__icon {
      line-height: 32px;
    }
    .userselect {
      flex: 1;
    }

    > span {
      display: block;
      width: 120px;
      text-align: right;
      padding-right: 15px;
    }
    > input {
      width: inherit;
      flex: 1;
      height: 32px;
      border-radius: 3px;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding-left: 10px;
      color: #333;
    }
  }
}
</style>
